/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.dao.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.ICharStoChoFixedDao;
import com.ce.pms.charge.model.CharStaChoDomain;
import com.ce.pms.charge.vo.CharStaChoFixedVO;
import com.ce.pms.charge.vo.CharStaChoMeterVO;
import com.ce.pms.charge.vo.ChargeStaChoVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：房间标准选用 收费类型为固定费用的持久层<br>
 * @author 罗军林 创建日期：2008-8-7
 */
public class CharStoChoFixedDaoImpl extends PmsBaseDaoImpl<CharStaChoDomain> implements ICharStoChoFixedDao{

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.dao.ICharStoChoFixedDao#queryCharStoCho(com.ce.pms.charge.vo.ChargeStaChoVO)
	 */
	public Page queryCharStoCho(ChargeStaChoVO condition) {
		Map<String, Object> parameter = new HashMap<String, Object>();

		StringBuffer sql = new StringBuffer("SELECT charstacho  FROM CharStaChoDomain charstacho,FlatDomain flat WHERE charstacho.house.id=flat.id ");
		sql.append(buildCondition(condition,parameter));
		sql.append(buildOrder(condition));
		return this.pagedNameQueryByPageNo(sql.toString(), condition.getPageNo(), condition.getPageSize(),parameter);
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.dao.ICharStoChoDao#addCharStaChoFixed
	 * (com.ce.pms.charge.vo.CharStaChoFixedVO)
	 */
	public void addCharStaChoFixed(CharStaChoFixedVO charStaChoFixedVO){
		String sql="INSERT INTO FEE_CHRG_STA_CHO(HOUSE_ID, CHARGE_ITEM_ID, CHARGE_STA_ID, START_TIME, END_TIME, CHARGE_AMT, CREATOR, SYSDATE)" +
				" VALUES(?,?,?,?,?,?,?,now()) ";
		List<Object[]> list=new ArrayList<Object[]>();
		String[] houseIds=charStaChoFixedVO.getHouseIds().split(",");
		for(int i=0;i<houseIds.length;i++){
			Object[] args={Long.valueOf(houseIds[i]),
					charStaChoFixedVO.getChargeItemID(),
					charStaChoFixedVO.getChargeStaID(),
					DateUtil.getFirstDayOfMonthofYear(charStaChoFixedVO.getStartTime()),
					DateUtil.getLastDayOfMonthOfYear(charStaChoFixedVO.getEndTime()),
					charStaChoFixedVO.getChargeAmt(),
					charStaChoFixedVO.getCreater()};
			list.add(args);

			if( ((i+1) % 50 ==0) || i==(houseIds.length-1) ){
				this.executeSQLBatch(sql, list);
				list.clear();
			}

		}
	}

	/**
	 * 功能描述:更新指定标准的费用。<br>
	 * @param chargeStaId
	 * @param chargeAmts 房间与费用的MAP<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void updateCharStoAmt(Long chargeStaId,BigDecimal chargeAmt){
		String sql="UPDATE FEE_CHRG_STA_CHO SET CHARGE_AMT=? WHERE CHARGE_STA_ID=?";
		executeSQL(sql, new Object[]{chargeAmt,chargeStaId});
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.dao.ICharStoChoDao#checkExistCharStoChoFixed
	 * (com.ce.pms.charge.vo.CharStaChoFixedVO)
	 */
	public Boolean checkExistCharStoChoFixed(CharStaChoFixedVO searchCondition,CharStaChoFixedVO oldCondition){
		Map<String, Long> parameter = new HashMap<String, Long>();
		StringBuffer sql=new StringBuffer("SELECT COUNT(*) FROM CharStaChoDomain char ")
		.append(" WHERE 1=1 ")
		.append(builderParameter(searchCondition,oldCondition,parameter));

		Query query=this.createNameQuery(sql.toString(), parameter);
		Long result=(Long)query.uniqueResult();

		return result>0;
	}

	/**
	 * 功能描述:构建唯一验证的查询条件<br>
	 * @param charStaChoMeterVO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-13<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String  builderParameter(CharStaChoFixedVO searchCondition,CharStaChoFixedVO oldCondition,Map<String, Long> parameter) {
		StringBuffer result=new StringBuffer();
		if(searchCondition!=null && searchCondition.getHouseId()!=null){
			parameter.put("hid", searchCondition.getHouseId());
			result.append(" AND char.house.keyword=:hid ");
		}
		if(searchCondition!=null && searchCondition.getChargeItemID()!=null){
			parameter.put("itemid", searchCondition.getChargeItemID());
			result.append(" AND char.chargeItem.keyword=:itemid ");
		}

		if(oldCondition!=null && oldCondition.getHouseId()!=null){
			parameter.put("oldHid", searchCondition.getHouseId());
			result.append(" AND char.house.keyword<>:oldHid ");
		}
		if(oldCondition!=null && oldCondition.getChargeItemID()!=null){
			parameter.put("oldItemid", oldCondition.getChargeItemID());
			result.append(" AND char.chargeItem.keyword<>:oldItemid ");
		}
		return result.toString();
	}

	/**
	 * 功能描述:<br>
	 * @param charStaChoFixedVO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-13<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private Map<String, Long> builderParaFixed(CharStaChoFixedVO charStaChoFixedVO) {
		Map<String, Long> paraFixed=new HashMap<String, Long>();
		paraFixed.put("hid", charStaChoFixedVO.getHouseId());
		paraFixed.put("itemid", charStaChoFixedVO.getChargeItemID());
		paraFixed.put("staid", charStaChoFixedVO.getChargeStaID());
//		paraFixed.put("Fixedid", charStaChoFixedVO.getFixedId());
		return paraFixed;
	}

	/**
	 * 功能描述:构建查询条件<br>
	 * @param condition
	 * @param paraFixed
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildCondition(ChargeStaChoVO condition,Map<String,Object> paraFixed){
		StringBuffer result=new StringBuffer();
		if(condition == null){
			return "";
		}

		result.append(" AND charstacho.chargeItem.chrgType=:chrgType ");
		paraFixed.put("chrgType", SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_FIXED);

		if(condition.getBuildingID() != null){
			result.append(" AND charstacho.house.building.keyword = :buildingID ");
			paraFixed.put("buildingID", condition.getBuildingID() );
		}

		if(condition.getChargeItemID() != null){
			result.append(" AND charstacho.chargeItem.keyword = :chargeItemID" );
			paraFixed.put("chargeItemID", condition.getChargeItemID() );
		}

		if(condition.getChargeStaID() != null){
			result.append(" AND charstacho.chargeSta.keyword = :chargeStaID ");
			paraFixed.put("chargeStaID", condition.getChargeStaID() );
		}

		if(condition.getHouseNumber() != null && condition.getHouseNumber().trim() != ""){
			result.append(" AND charstacho.house.houseNumber like :houseNumber ");
			paraFixed.put("houseNumber", "%"+condition.getHouseNumber()+"%");
		}

		return result.toString();
	}

	/**
	 * 功能描述:构建排序条件<br>
	 * @param condition
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildOrder(ChargeStaChoVO condition){
		if(condition.getSortStr()==null || condition.getSortStr().trim().length()<1){
			return " ORDER BY charstacho.house.building.seqNo,flat.floor,charstacho.house.houseNumber ";
		}else if("SYSDATE".indexOf(condition.getSortStr().trim().split(" ")[0].toUpperCase())>=0){
			return " ORDER BY charstacho.sysInfo.sysDate DESC" ;
		}else{
			return " ORDER BY " + condition.getSortStr() ;
		}
	}



}
